package com.yxy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yxy.common.PageResult;
import com.yxy.constant.StatusConstant;
import com.yxy.context.BaseContext;
import com.yxy.domain.dto.FeedBackDTO;
import com.yxy.domain.dto.FeedBackPageQueryDTO;
import com.yxy.domain.po.FeedBack;
import com.yxy.domain.vo.FeedBackPageQueryVO;
import com.yxy.domain.vo.FeedbackVO;
import com.yxy.mapper.FeedBackMapper;
import com.yxy.service.FeedBackService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class FeedBackServiceImpl implements FeedBackService {

    private final FeedBackMapper feedBackMapper;

    public FeedBackServiceImpl(FeedBackMapper feedBackMapper) {
        this.feedBackMapper = feedBackMapper;
    }

    /**
     * 反馈
     * @param feedBackDTO
     */
    @Override
    public void feedBack(FeedBackDTO feedBackDTO) {
        FeedBack feedBack = BeanUtil.copyProperties(feedBackDTO, FeedBack.class);
        feedBack.setUserId(BaseContext.getCurrentId());
        feedBack.setFeedBackTime(LocalDateTime.now());
        feedBack.setStatus(StatusConstant.REVIEW);
        feedBackMapper.insert(feedBack);
    }

    /**
     * 查询反馈
     * @param feedBackPageQueryDTO
     * @return
     */
    @Override
    public PageResult queryFeedBack(FeedBackPageQueryDTO feedBackPageQueryDTO) {
        PageHelper.startPage(feedBackPageQueryDTO.getPage(), feedBackPageQueryDTO.getPageSize());
        feedBackPageQueryDTO.setUserId(BaseContext.getCurrentId());
        Page<FeedBackPageQueryVO> page = feedBackMapper.queryFeedBack(feedBackPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 获取反馈
     * @param id
     * @return
     */
    @Override
    public FeedbackVO getFeedbackById(int id) {
        FeedBack feedBack = feedBackMapper.getFeedbackById(id, BaseContext.getCurrentId());
        FeedbackVO vo = BeanUtil.copyProperties(feedBack, FeedbackVO.class);
        return vo;
    }
}
